package com.jorado.etl.taskhandles;

import com.jorado.search.core.config.Solr;
import com.jorado.search.core.config.SolrConfig;
import com.jorado.search.core.exception.NoAssignSolrException;
import com.jorado.search.core.task.BaseHandler;
import com.jorado.search.solr.client.SolrjClient;
import com.jorado.search.solr.client.SolrjClientPool;
import com.jorado.search.solr.client.SolrjOption;
import org.springframework.context.annotation.Description;
import org.springframework.util.CollectionUtils;

/*
 * Created by len.zhang on 2018/4/17.
 *
 */
@Description("Word2vec模型训练任务")
public final class Word2vecTrainHandler extends BaseHandler {

    @Override
    protected void exec(Object... args) {

//        try {
//
//            String tmpTrain = RemoteSettings.word2vecTrainFile() + ".temp";
//            String tmpModel = RemoteSettings.word2vecModelFile() + ".temp";
//
//            //删除临时文件
//            IOUtils.delete(tmpTrain, tmpModel);
//
//            //合并增量文件并去重后另存
//            List<String> lines0 = IOUtils.readLines(RemoteSettings.word2vecIncrementTrainFile());
//            List<String> lines1 = IOUtils.readLines(RemoteSettings.word2vecTrainFile());
//            List<String> lines = FileUtils.margin(lines0, lines1);
//
//            //如果记录数没有发生变更，返回
//            if (lines.size() == lines1.size()) {
//                EventClient.getDefault().asyncSubmitLog("Word2vec train over,no have new record!!!");
//                return;
//            }
//
//            IOUtils.writeLines(tmpTrain, lines);
//
//            EventClient.getDefault().asyncSubmitLog("Word2vec train beging...");
//            TrainerProxy trainerProxy = new TrainerProxy(tmpTrain, tmpModel);
//            Word2Vec word2Vec = trainerProxy.setEventCallBack().train();
//
//            //训练成功后删除旧文件
//            IOUtils.delete(RemoteSettings.word2vecIncrementTrainFile(), RemoteSettings.word2vecTrainFile(), RemoteSettings.word2vecModelFile());
//            FileUtils.renameFile(tmpTrain, RemoteSettings.word2vecTrainFile());
//            FileUtils.copyFile(tmpModel, RemoteSettings.word2vecModelFile());
//
//            //上传到dfs上
//            HDFSClient dfsClient = new HDFSClient(RemoteSettings.dfsHost());
//            dfsClient.upload(RemoteSettings.word2vecModelFile(), RemoteSettings.word2vecDfsModelFile());
//
//            EventClient.getDefault().asyncSubmitLog(String.format("Word2vec train over,size:[%d],path:[%s],new record:[%d]", word2Vec.getVectorModel().size(), RemoteSettings.word2vecModelFile(), lines.size() - lines1.size()));
//
//        } catch (IOException ex) {
//            EventClient.getDefault().asyncSubmitException("Train io error", ex);
//        } catch (Exception ex) {
//            EventClient.getDefault().asyncSubmitException("Train error", ex);
//        }
    }

    private SolrjOption assignPositionSolr() {

        Solr solr = SolrConfig.getInstance().getSolr();

        if (null == solr || CollectionUtils.isEmpty(solr.getHost())) {
            throw new NoAssignSolrException();
        }

        SolrjClient solrjClient = SolrjClientPool.newClient(solr.getHost(), solr.isCloud());
        return new SolrjOption(solr.getCollection(), solrjClient);
    }

}
